﻿using Glorysoft.Auto.Xfw;
using Glorysoft.EAP.Core;
using Glorysoft.EAP.Core.Entities;
using Glorysoft.EAP.Core.Entities.Odyssey;
using Glorysoft.EAP.Core.Enums;
using Glorysoft.EAP.RC.EAPClient;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json.Linq;
using Serilog.Core;
using System;
using System.Collections.Generic;
using System.Text;

namespace Glorysoft.EAP.Contracts.Odyssey
{
  public  class OdysseyClient
    {
        protected readonly Logger CimLogger;
        protected readonly string LotBaseInfo = "lotBaseInfo";
        protected readonly string ParameterSet = "parameterSet";
        EAPClientService ClientService = null;
        SoapClient soapClient;
        public string Name => GetType().Name;
        public OdysseyClient()
        {
            CimLogger = XfwContext.LogManager.DbLogger;

            ClientService = new EAPClientService();
        }
        /// <summary>
        /// Odyssey 出站
        /// </summary>
        /// <param name="lotBaseInfo">基础信息</param>
        /// <param name="parameterSet">参数信息</param>
        /// <returns></returns>
        public TrackOutResponse TrackOut(string lotBaseInfo, string parameterSet)
        {
            TrackOutResponse _return = new TrackOutResponse();
            if (Dict.Current.EAPRunMode == RunMode.ManualOperation)
            {
                var messages = TranslateMsg.GetTranslation(CItems.ManualOperation);
                ClientService.ShowMessage("", MessageType.Info, messages, MessageOrigin.ODYSSEY);
                CimLogger.Warning($"{Name}.TrackOut()  The EAP has been switched Manual Operation ");
                return _return;
            }
            var baseurl = Dict.Current.OdysseyIP;//"http://10.190.220.131/mycim2/services/JobManagementWebService?wsdl";
            soapClient = new SoapClient(baseurl);
            Dictionary<string, string> trackout = new Dictionary<string, string>();
            trackout.Add(LotBaseInfo, lotBaseInfo);
            trackout.Add(ParameterSet, parameterSet);
 
            //发送http接口
            var ret = soapClient.HttpRequestList("TrackOut", trackout);
            CimLogger.Information($" OdysseyClient<OdysseyClient.TrackOut() Execute. Text:{ret}");
            JObject jsonObj = JObject.Parse(ret);
            var result = jsonObj["success"].ToString();
            var resultmsg = jsonObj["msg"].ToString();
            var lotId = jsonObj["lotId"].ToString();
            var equipmentId = jsonObj["equipmentId"].ToString();
            //读取本地json文件
            //string ret = getJsonData("./MESInterfaceTest/" + request.EQUIPMENTID + "/TrackIn_Success.json");
            //反序列化Response
            if (result == "True")
            {
                _return.success = result;
                _return.msg = resultmsg;
            }
            else
            {
                _return.success = result;
                _return.msg = resultmsg;
            }
            return _return;
        }
    }
}
